var vm = new Vue({
    el: '#profileInfo', //el指定一个页面中已存在的DOM元素来挂载VUE实例
    data: {
        hotListImg: {},   //热卖的四张图片
        catalogue: {}, //目录
        categoryNum: {
            groupid: "1"
        },             //目录编号    1 2 4 5 6 7 8
        userByIdInfo: {}, //用户信息
        name: "",      //登陆用户名
        userDelivery: {}, //收货信息
        updateuserDelivery: {        //更新的用户信息
            ismain: false,
            isdel: "0",
            userid: JSON.parse(localStorage.getItem("userId"))
        },
        updateByIdInfo:{},
        userFavorGoods:{}  //用户收藏的商品

    },
    created: function () {
        // this.hostlist();
        this.findById();
        // this.catalogueList();
        this.userDeliveryFindByUserId();

        // this.findById();
        // var data = localStorage.getItem("userInfo");
        // this.userByIdInfo = JSON.parse(data).data;
        // console.log(this.userByIdInfo);
        this.userFavorGoodsInfo();

        //加载热门商品
        var data = localStorage.getItem("hotList");
        this.hotListImg = JSON.parse(data);
        //加载目录
        var data1 = localStorage.getItem("categoryNum");
        this.catalogue = JSON.parse(data1);

        var username = localStorage.getItem("userName");
        this.name = JSON.parse(username);
        console.log(this.userDelivery)
    },
    methods: {

        // hostlist:function(){
        //     //发送请求 返回最热门发布的四张图片
        //     axios.request({
        //         url:`/goods/search/hotlist`
        //     }).then(res=>{
        //         this.hotListImg=res.data;
        //         localStorage.setItem("hotList", JSON.stringify(this.hotListImg))
        //     })
        // },

        // catalogueList:function(){
        //     //发送请求 目录
        //     axios.request({
        //         url:`/categorys`
        //     }).then(res=>{
        //         this.catalogue=res.data;
        //         //将后端接口返回的数据  存到缓存中
        //         localStorage.setItem("categoryNum",JSON.stringify(res.data.data))
        //     })
        // },


        //通过目录id找 以下的商品
        findByCategory: function (category = 1) {
            localStorage.setItem("categoryOne", JSON.stringify(category));
            //发送请求查询目录为1的商品

            axios.request({
                url: `/goods/searchOne/${category}`,
            }).then(res => {
                window.location.href = "/view/productList";
                localStorage.setItem("categoryNumInfo", JSON.stringify(res.data));
            });
        },
        //查看商品详情
        goodsdetail: function (id = 1) {
            console.log(id);
            //设置缓存id。
            localStorage.setItem("thisgoodsId", JSON.stringify(id));
            this.thisgoodsId = id;
            axios.request({
                url:`/goods/updateVisitors/${id}/${JSON.parse(localStorage.getItem("userId"))}`
            }).then(res=>{
                window.location.href="/view/goodsDetail"
            })
        },

        //渲染收货地址
        userDeliveryFindByUserId: function () {
            axios.request({
                url: `/delivery/userDelivery/${JSON.parse(localStorage.getItem("userId"))}`,
            }).then(res => {
                this.userDelivery = res.data;
                localStorage.setItem("userDelivery", JSON.stringify(res.data));
            });
        },

        testuserDelivery: function () {
            console.log(this.userDelivery);
        },
        //设置收货地址
        setDeafult: function (id = 1) {
            layer.msg("亲爱的您已设置为收货地址" + id + "为默认");
            axios.request({
                url: `/delivery/setDefault/${id}/${JSON.parse(localStorage.getItem("userId"))}`
            }).then(res => {
                this.userDeliveryFindByUserId();
            })
        },


        //保存收穫地址  如果選擇了 this.updateuserDelivery.ismain  ==true  勾选调用 setDeafult（存入后端返回的自增长后的id）
        // 先清空全部 在设置该id为默认收货地址

        //  else   : 直接添加    后清空数据
        savedelivery: function () {
            console.log(this.updateuserDelivery);
            console.log(this.updateuserDelivery.ismain);
            if (this.updateuserDelivery.detaildelivery != null &&
                this.updateuserDelivery.username != null &&
                this.updateuserDelivery.phone != null &&
                this.updateuserDelivery.userdelivery != null &&
                this.updateuserDelivery.postal != null
            ) {
                if (this.updateuserDelivery.ismain == false) {
                    axios.request({
                        url: `/delivery/saveNoSelect`,
                        method: "post",
                        headers: {
                            'Content-Type': 'application/json' //重点
                        },
                        data: this.updateuserDelivery
                    }).then(res => {
                        this.userDeliveryFindByUserId();
                        this.updateuserDelivery.username = "";
                        this.updateuserDelivery.phone = "";
                        this.updateuserDelivery.userdelivery = "";
                        this.updateuserDelivery.postal = "";
                        this.updateuserDelivery.detaildelivery = "";
                    })
                } else {
                    axios.request({
                        url: `/delivery/save`,
                        method: "post",
                        headers: {
                            'Content-Type': 'application/json' //重点
                        },
                        data: this.updateuserDelivery
                    }).then(res => {
                        this.setDeafult(res.data.data);
                        this.userDeliveryFindByUserId();
                        this.updateuserDelivery.username = "";
                        this.updateuserDelivery.phone = "";
                        this.updateuserDelivery.userdelivery = "";
                        this.updateuserDelivery.postal = "";
                        this.updateuserDelivery.detaildelivery = "";
                    })
                }

            } else {
                layer.msg("请检查必要文本框是否输入完全")
            }
        },

        //逻辑删除
        deleteDelivery(id = 1) {
            axios.request({
                url: `/delivery/delete/${id}`
            }).then(res => {
                // var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(); //再执行关闭.
                this.userDeliveryFindByUserId();
            })
        },


        //通过当前登陆id 去获取数据
        findById: function () {
            axios.request({
                url: `/user/${JSON.parse(localStorage.getItem("userId"))}`
            }).then(res => {
                this.userByIdInfo=res.data.data;
            })
        },

        /**
         * 保存用户
         */
        saveUser:function () {
            console.log(this.userByIdInfo);
            axios.request({
                url:`/user/update/${JSON.parse(localStorage.getItem("userId"))}`,
                method:"put",
                data:this.userByIdInfo
            }).then(res=>{
                layer.msg("保存成功");
                this.findById();
            })
        },

        /**
         * 修改密码
         */
        resetPassword:function (id=1) {
            layer.msg("正在重置用户id为:"+id+"的用户");
            if(this.updateByIdInfo.updatepassword1!=this.updateByIdInfo.updatepassword2){
                layer.msg("两次输入密码不一致")
            }else {
                layer.msg("可以修改");
                this.userByIdInfo.password=this.updateByIdInfo.updatepassword1;
                axios.request({
                    url:`/user/updatePassword/${JSON.parse(localStorage.getItem("userId"))}`,
                    method:"put",
                    data:this.userByIdInfo
                }).then(res=>{
                    layer.msg("修改成功");
                    this.findById();
                })
            }
            console.log(this.updateByIdInfo)
        },

//注销
        logout:function () {
            layer.msg("注销倒计时......")
            sessionStorage.removeItem("userInfo");
            localStorage.clear();
            window.location.href="/view/logout"
        },
        //用户收藏的全部商品
        userFavorGoodsInfo:function () {
            axios.request({
                url:`/userFavorGoods/findByUserIdFavorGoods/${JSON.parse(localStorage.getItem("userId"))}`
            }).then(res=>{
                this.userFavorGoods=res.data.data;
            })
        }

    },
    filters: {
        judge(value) {
            var returnVal = "";
            switch (value) {
                case "1":
                    returnVal = "数码产品";
                    break;
                case "2":
                    returnVal = "运动户外";
                    break;
                case "3":
                    returnVal = "家居家纺";
                    break;
                case "4":
                    returnVal = "家具家装";
                    break;
                case "5":
                    returnVal = "生活超市";
                    break;
                case "6":
                    returnVal = "唯品国际";
                    break;
                case "7":
                    returnVal = "医药健康";
                    break
            }
            return returnVal;
        },
        judsex(value) {
            var returnVal = "";
            switch (value) {
                case 1:
                    returnVal = "男";
                    break;
                case 0:
                    returnVal = "女";
                    break;
            }
            return returnVal;
        }
    }
});